package cn.kgc.jdbc.dao;

import cn.kgc.jdbc.entity.Student;
import cn.kgc.jdbc.utils.DruidUtil;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/8/28
 */
public class StudentDaoImpl implements StudentDao {

    @Override
    public Integer insertStudent(Student student) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "insert into student(stu_name,clazz_id)values(?,?)";
        try {
            int update = queryRunner.update(sql, student.getStuName(), student.getClazzId());
            return  update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer deleteById(Integer id) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "delete from student where id = ?";
        try {
            int delete = queryRunner.update(sql, id);
            return  delete;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer updateStudentById(Integer id, long clazzId) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "update student set clazz_id = ? where id = ?";
        try {
            int  update = queryRunner.update(sql, clazzId, id);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }

    @Override
    public List<Student> findAllStudents() {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select student.id,student.clazz_id,student.stu_name,clazz.clazz_name from student,clazz where student.clazz_id = clazz.id";
        //实现驼峰命名的转换器
        BasicRowProcessor basicRowProcessor = new BasicRowProcessor(new GenerousBeanProcessor());

        try {
            List<Student> students = queryRunner.query(sql, new BeanListHandler<Student>(Student.class, basicRowProcessor));
            return  students;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Student findByName(String name) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select * from student where stu_name = ?";
        //实现驼峰命名的转换器
        BasicRowProcessor basicRowProcessor = new BasicRowProcessor(new GenerousBeanProcessor());

        try {
            Student student = queryRunner.query(sql, new BeanHandler<Student>(Student.class, basicRowProcessor), name);
            return  student;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
